using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class GameManager : Singleton<GameManager>
{
    public PlayerData playerData;
    public GameState currentState;

    private SceneLoader sceneLoader;
    private UIManager uiManager;
    private SaveLoadManager saveLoadManager;

    private void Awake()
    {
        // Initialize managers and dependencies
        sceneLoader = GetComponent<SceneLoader>();
        uiManager = FindObjectOfType<UIManager>();
        saveLoadManager = GetComponent<SaveLoadManager>();

        // Load player data
        if (saveLoadManager != null)
        {
            playerData = saveLoadManager.LoadPlayerData();
            if (playerData == null)
            {
                playerData = new PlayerData();
            }
        }
        else
        {
            playerData = new PlayerData();
        }
    }

    private void Start()
    {
        // Initialize game state machine
        SwitchState(GameState.Town);
        if (sceneLoader != null)
        {
            sceneLoader.LoadTownScene();
        }
    }

    public void SwitchState(GameState newState)
    {
        currentState = newState;
        // Trigger state change event
        EventManager.TriggerEvent(new GameStateChangedEvent(newState));
    }

    // Start new dungeon run
    public void StartNewDungeonRun()
    {
        SwitchState(GameState.Dungeon);
        if (sceneLoader != null)
        {
            sceneLoader.LoadDungeonScene();
        }
        EventManager.TriggerEvent(new DungeonStartedEvent());
    }

    // Return to town from dungeon
    public void ReturnToTown()
    {
        SwitchState(GameState.Town);
        if (sceneLoader != null)
        {
            sceneLoader.LoadTownScene();
        }
        EventManager.TriggerEvent(new ReturnedToTownEvent());
    }
}

public enum GameState
{
    Town,
    Dungeon,
    Combat,
    Dialogue,
    Shop,
    Crafting
}
